<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\OrgDepartment;
use App\Admin\Repositories\OrgDepartmentOwner;
use App\Admin\Repositories\OrgTeam;
use App\Admin\Repositories\OrgTeamOwner;
use App\Admin\Repositories\TeamMember;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Illuminate\Support\Facades\Auth;

class TeamMemberController extends AdminController
{

    public function index(Content $content)
    {
        return $content
            ->header('小组成员')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new TeamMember(['department', 'team']), function (Grid $grid) {
            if (!$this->isAdmin()){
                $department_id_list = $team_id_list = [];
                $owner_department_list = app(OrgDepartmentOwner::class)->getDepartmentListByOwnerUid(Auth::guard('admin')->id());
                if ($owner_department_list){
                    $department_id_list = $owner_department_list->pluck('department_id')->toArray();
                }
                $owner_team_list = app(OrgTeamOwner::class)->getTeamListByOwnerUidAndDepartmentIds(Auth::guard('admin')->id(), $department_id_list);
                if ($owner_team_list){
                    $team_id_list = $owner_team_list->pluck('team_id')->unique()->toArray();
                }

                $grid->model()
                    ->whereIn('team_id', $team_id_list);
            }
            $grid->model()->orderBy('member_id', 'desc');

            $grid->column('member_id', '组员ID')->sortable();
            $grid->column('member_name', '姓名');
            $grid->column('email');
            $grid->column('phone_number');
            $grid->column('team.name', '所属小组')->label();
            $grid->column('department.name', '所属部门')->label('success');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->column('status', '是否在职')->switch('green');
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();

                if ($this->isAdmin()){
                    $department_list = app(OrgDepartment::class)->getOptionList();
                }else{
                    $owner_team_list = app(OrgTeamOwner::class)->getTeamListByOwnerUid(Auth::guard('admin')->id());
                    $department_id_list = $owner_team_list->pluck('department_id')->unique()->toArray();
                    $department_list = app(OrgDepartment::class)->getOptionListByDepartmentIds($department_id_list);
                }

                $filter->equal('department_id', '部门')
                    ->width(2)
                    ->select($department_list)
                    ->load('team_id', '/api/team');
                $filter->equal('team_id', '小组')
                    ->width(2)
                    ->select();
                $filter->equal('phone_number', '手机号')->width(2);
                $filter->equal('email', '邮箱')->width(3);
                $filter->equal('status', '是否在职')->width(2)->select(
                    \App\Models\TeamMember::$status_list
                );
            });

            $grid->disableRowSelector();
            $grid->disableViewButton();
            $grid->enableDialogCreate();
            $grid->showQuickEditButton();
            $grid->disableEditButton();
            $grid->setActionClass(Grid\Displayers\Actions::class);
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TeamMember(), function (Form $form) {
            $form->text('member_name')
                ->maxLength(10, '最长不超过10个字符')
                ->required();
            $form->select('department_id', '所属部门')->options(app(OrgDepartment::class)->getOptionList())
                ->load('team_id', '/api/team')->required();
            $form->select('team_id', '所属小组')->required();
            $form->switch('status', '是否在职')->default(\App\Models\TeamMember::MEMBER_STATUS_ON)->color('#21b978');
            $form->email('email');
            $form->mobile('phone_number');
        });
    }

    public function api()
    {
        $department_id = request('q');

        if (!$this->isAdmin()){
            $owner_department_list = app(OrgDepartmentOwner::class)->getDepartmentListByOwnerUid(Auth::guard('admin')->id());
            $department_id_list = [];
            if ($owner_department_list){
                $department_id_list = $owner_department_list->pluck('department_id')->toArray();
            }

            if (!empty($department_id_list)){
                if (in_array($department_id, $department_id_list)){
                    return app(OrgTeam::class)->getOptionsByDepartmentId($department_id);
                }
            }

            $owner_team_list = app(OrgTeamOwner::class)->getTeamListByOwnerUidAndDepartmentId(Auth::guard('admin')->id(), $department_id);
            return app(OrgTeam::class)->getOptionsByTeamIds($owner_team_list->pluck('team_id')->unique()->toArray());
        }

        return app(OrgTeam::class)->getOptionsByDepartmentId($department_id);
    }
}
